import { fileURLToPath, URL } from 'node:url'
import path from 'path'
import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'

import { viteMockServe } from 'vite-plugin-mock'
import createVitePlugins from './vite/plugins'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// https://vitejs.dev/config/
export default defineConfig(({command,mode})=>{
  const envConfig = loadEnv(mode, process.cwd())
  const { VITE_BASE_URL ,VITE_OUTPUT_DIR,VITE_USE_MOCK}  = envConfig
  console.log(VITE_BASE_URL)
  return {
  base: VITE_BASE_URL,
  define: {
    'process.env': envConfig
  },
  plugins: [
    createVitePlugins(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [IconsResolver(),ElementPlusResolver()],
    }),
    Icons({
      compiler: 'vue3',
      autoInstall: true,
    }),
    viteMockServe({
      mockPath: "./src/mock/",
      prodEnabled:false,
      localEnabled:VITE_USE_MOCK
    }),
  ],
  build: {
    outDir: VITE_OUTPUT_DIR
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/assets/css/variables.scss" as *;`,
      },
    },
  },
  resolve: {
    alias: {
      '~': path.resolve(__dirname, './'),
      '@':  path.resolve(__dirname, './src')
    },
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  },
  server: {
    port: 8077,
    host: true,
    open: true,
    /** 代理配置*/
    proxy: {
      '/cat': {
        target: 'https://sacasaca.com',
        changeOrigin: true,
        rewrite: (p) => p.replace(/^\/cat/, '/cat')
      }
    }
  },
}
})
